<include file="common:__head__"/>
<div class="permissions-tabs">
    <a href="javascript:;" onclick="window.history.back();" style="margin: 18px 0 0 4px;"><img src="./Public/seller/images/permissions6.png"></a>
    <a class="role tabs-bg" href="javascript:;" mark="info">商品管理</a>
    <a class="role" href="javascript:;" mark="images">商品相册</a>
    <a class="role" href="javascript:;" mark="spec">商品规格</a>
</div>


<form class="form-horizontal" id="editGoodsForm" action="{:U('Good/edit')}" method="post">
    <input type="hidden" value="{$good['id']}" name="good_id">
    <div class="info">
        <div class="permissions-content">
            <div class="permissions-le">
                <div class="add-admin-ipt">
                    <span><i>*</i>商品名称:</span>
                    <input type="text" name="g_name" id="g_name" placeholder="请填写商品名称" class="w200" value="{$good['g_name']}"/>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>商品图片:</span>
                    <div class="add-admin-opt">
                        <div class="input-file-show">
                        <span class="show">
                            <a id="img_a" target="_blank" class="nyroModal" rel="gal" href="">
                                <i id="img_i" class="fa fa-picture-o" onmouseover="layer.tips('<img src={$good[img]}>',this,{tips: [1, '#fff']});" onmouseout="layer.closeAll();"></i>
                            </a>
                        </span>
                        <span class="type-file-box">
                            <input type="text" id="image" name="original_img" value="{$good[img]}" class="type-file-text">
                            <input type="hidden" name="thumb" id="thumb" value="">
                            <input type="button" name="button" id="button1" value="选择上传..." class="type-file-button">
                            <input class="type-file-file" onClick="GetUploadify(1,'','good','img_call_back')" size="30" hidefocus="true" nc_type="change_site_logo" title="点击前方预览图可查看大图，点击按钮选择文件并提交表单后上传生效">
                        </span>
                        </div>
                        <span class="err"></span>
                        <p class="notic">品牌图片，最佳显示尺寸为148*28像素</p>
                    </div>
                </div>


                <div class="add-admin-ipt">
                    <span><i>*</i>商品分类:</span>
                    <select name="cat_id" id="cat_id">
                        <option value="0">-- 请选择分类 --</option>
                        <volist name="categories" id="category">
                            <option value="{$category['id']}" <if condition="$category['id'] eq $good['cate']">selected</if> >{$category['c_name']}</option>
                        </volist>
                    </select>
                </div>


                <div class="add-admin-ipt">
                    <span><i>*</i>商品品牌:</span>
                    <select name="brand_id" id="brand_id">
                        <option value="0">-- 请选择 --</option>
                        <volist name="brands" id="brand">
                            <option value="{$brand['id']}" <if condition="$good[brand_id] eq $brand['id']">selected</if> >{$brand['b_name']}</option>
                        </volist>
                    </select>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>商品简介:</span>
                    <textarea name="brief" id="brief" cols="40" rows="3" class="txt-area">{$good['brief']}</textarea>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>商品货号:</span>
                    <input type="text" name="good_sn" id="good_sn" placeholder="请填写商品货号" class="w200" value="{$good['good_sn']}"/>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>本店售价:</span>
                    <input type="text" name="price" id="price" placeholder="请填写商品售价" class="w200" onKeyUp="this.value=this.value.replace(/[^\d.]/g,'')" onpaste="this.value=this.value.replace(/[^\d.]/g,'')" value="{$good['price']}"/>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>市场价:</span>
                    <input type="text" name="market_price" id="market_price" placeholder="请填写市场价" class="w200" onKeyUp="this.value=this.value.replace(/[^\d.]/g,'')" onpaste="this.value=this.value.replace(/[^\d.]/g,'')" value="{$good['market_price']}"/>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>销量:</span>
                    <input type="text" name="sale_num" id="sale_num" placeholder="请填写销量" class="w200" onKeyUp="this.value=this.value.replace(/[^\d.]/g,'')" onpaste="this.value=this.value.replace(/[^\d.]/g,'')" value="{$good['sale_num']}"/>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>佣金:</span>
                    <input type="text" name="commission" id="commission" placeholder="请填写佣金" class="w200" onKeyUp="this.value=this.value.replace(/[^\d.]/g,'')" onpaste="this.value=this.value.replace(/[^\d.]/g,'')" value="{$good['commission']}"/>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>重量:</span>
                    <input type="text" name="weight" id="weight" placeholder="请填写销量" class="w200" onKeyUp="this.value=this.value.replace(/[^\d.]/g,'')" onpaste="this.value=this.value.replace(/[^\d.]/g,'')" value="{$good[weight]}"/>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>库存:</span>
                    <input type="text" name="stock" id="stock" placeholder="请填写库存" class="w200" onKeyUp="this.value=this.value.replace(/[^\d.]/g,'')" onpaste="this.value=this.value.replace(/[^\d.]/g,'')" value="{$good[stock]}"/>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>装箱件数:</span>
                    <input type="text" name="per_num" id="per_num" placeholder="请填写装箱件数" class="w200" onKeyUp="this.value=this.value.replace(/[^\d.]/g,'')" onpaste="this.value=this.value.replace(/[^\d.]/g,'')" value="{$good[per_num]}"/>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>是否可以退款:</span>
                    <select name="back_id" id="back_id">
                        <option value="0" <if condition="$good['is_back'] eq 0">selected</if> >是</option>
                        <option value="1" <if condition="$good['is_back'] eq 1">selected</if> >否</option>
                    </select>
                </div>

                <div class="add-admin-ipt back_explain" >
                    <span><i>*</i>不能退款原因:</span>
                    <textarea rows="3" cols="80" name="noback_reason" class="input-txt" id="noback_reason">{$good['noback_reason']}</textarea>
                </div>

                <div class="add-admin-ipt">
                    <span><i>*</i>商品详细描述:</span>
                    <textarea name="info" id="goods_content" style="background: red;float: right;">{$good[info]}</textarea>
                </div>
            </div>
            <div class="permissions-ri">
                <a href="" class="permissions-help">帮助和服务</a>
            </div>
        </div>
    </div>

    <div class="images" style="display: none;">
        <div class="permissions-content">
            <div class="permissions-le">
                <foreach name="pics" item="pic">
                    <td>
                        <div class="goods_xc" style="width:100px; text-align:center; margin: 5px; display:inline-block;">
                            <input type="hidden" value="{$pic['img']}" class="goods_imgs">
                            <a href="{$pic['img']}" onclick="" target="_blank">
                                <img src="{$pic['img']}" width="100" height="100">
                            </a>
                            <br>
                            <a href="javascript:void(0)" onclick="edit_clear_pic(this,'{$pic[img]}', '{$pic[id]}')">删除</a>
                        </div>
                    </td>
                </foreach>

                <div class="goods_xc" style="width:100px; text-align:center; margin: 5px; display:inline-block;">
                    <input type="hidden" name="goods_images[]" value="" class="goods_imgs"/>
                    <a href="javascript:void(0);" onClick="GetUploadify(5,'','goods','call_back2');">
                        <img src="__PUBLIC__/images/img_upload.png" width="100" height="100" />
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="spec" style="display: none;">
        <div class="permissions-content">
            <div class="permissions-le">
                <dl class="spec_box">
                    <dt class="tit">
                        商品规格&nbsp; <a class="plus_spec" href="javascript:;"><strong>[+]</strong></a>
                    </dt>
                    <dd class="opt">
                        <div class="spec_box">

                            <volist name="goodSpec" id="x" key="k">
                                <div style="margin-bottom:10px;" class="spec_k{$k}">
                                    <select name="spe_{$k}" class="small form-control spec{$k} spec_select">
                                        <option value="0">- 选择规格属性 -</option>
                                        <volist name="spec" id="vo">
                                            <option value="{$vo['id']}" <if condition="$x['spec_id'] eq $vo['id']">selected</if> >{$vo['s_name']}</option>
                                        </volist>
                                    </select>
                                    <a class="p_plus" href="javascript:;" for="{$k}"><strong>[+]</strong></a>
                                </div>
                                <div class="alisth{$k}" style="margin-bottom:10px;">
                                    <volist name="x['spec_val']" id="spec_val" >
                                        <a>
                                            <input type="text" name="spec_{$k}[]" style="width:80px;" placeholder="规格值" value="{$spec_val}"/>&nbsp;
                                            <strong onclick="$(this).parent().remove();" style="margin-right:10px;">[-]</strong>
                                        </a>
                                    </volist>
                                </div>
                            </volist>

                            <div style="margin-bottom:10px;" class="spec_k0">
                                <select name="spe_0" class="small form-control spec0 spec_select">
                                    <option value="0">- 选择规格属性 -</option>
                                    <volist name="spec" id="vo">
                                        <option value="{$vo['id']}">{$vo['s_name']}</option>
                                    </volist>
                                </select>
                                <a class="p_plus" href="javascript:;" for="0"><strong>[+]</strong></a>
                            </div>
                            <div class="alisth0" style="margin-bottom:10px;"></div>
                        </div>
                    </dd>
                </dl>
            </div>
        </div>
    </div>

    <div style="padding: 0 0 10px 10px;">
        <span></span>
        <button class="btn-lg btn-blue" id="submit_btn">保存</button>
    </div>
</form>
<script type="text/javascript">
    $(function(){


        //退款理由
        $("#back_id").change(function () {
            if($(this).val() == 1){
                $(".back_explain").show();
            }else{
                $(".back_explain").hide();
            }
        });


        //tag切换
        $('.permissions-tabs a').click(function() {
            $('.permissions-tabs a').removeClass('tabs-bg');
            $(this).addClass('tabs-bg');
            var mark = $(this).attr('mark');
            $('.images,.spec,.info').hide();
            $('.'+mark).show();
        });



        //添加商品规格的值的时候,动态添加input框
        $('body').on('click','.p_plus', function() {
            var l = $(this).attr("for");
            var my_parent = $(this).parent().parent().parent();
            var select_val = my_parent.find('.spec' + l).val();
            if (select_val != 0) {
                var str = '<a>' +
                        '<input type="text" name="spec_' + l + '[]" style="width:80px;" placeholder="规格值">&nbsp;' +
                        '<strong onclick="$(this).parent().remove();" style="margin-right:10px;">[-]</strong>' +
                        '</a>';
                my_parent.find(".spec_box").find('.alisth' + l).append(str);
            }else{
                layer.alert('请先选择规格属性!', {icon: 2,time:1000});
            }
        });

        //商品规格
        $('.plus_spec').click(function() {
            var spec_i = $('.spec_select').length;
            spec_i += 1;
            $('.p_plus').show();
            var html_str = '<div class="spec_box">' +
                    '<div class="spec_k'+ spec_i +'" style="margin-bottom:10px;">' +
                    '<select name="spe_'+ spec_i +'" class="small form-control spec'+ spec_i +' spec_select">' +
                    '<option value="0">- 选择规格属性 -</option>' +
                    '<volist name="spec" id="vo">' +
                    '<option value="{$vo[id]}">{$vo[s_name]}</option>' +
                    '</volist>' +
                    '</select>' +
                    '<a class="p_plus" href="javascript:;" for="'+ spec_i +'"><strong>[+]</strong></a>' +
                    '&nbsp;<strong onclick="$(this).parent().parent().remove()">[-]</strong>' +
                    '</div>' +
                    '<div class="alisth'+ spec_i +'" style="margin-bottom:10px;"></div>' +
                    '</div>';
            $(this).parents(".tit").next('.opt').append(html_str);
        });

        //添加商品
        $("#submit_btn").click(function() {
            // 规格类的个数
            var cate = $("#cat_id").val();          // 分类
            var brand = $("#brand_id").val();       // 品牌
            var img = $('#imagetext').val();        // 主图
            var per_num = $(".per_num").val();      // 装箱件数
            var sale_num = $(".sale_num").val();    // 销量
            var stock = $(".stock").val();          // 库存
            var weight = $('.weight').val();        // 重量
            var price = $('.price').val();          // 售价
            var g_name = $('#g_name').val();        // 名称
            var brief = $('#brief').val();          // 简介
            var market_price = $('.market_price').val(); // 市场价
            var goods_imgs = $('.goods_imgs').val(); // 相册
            var back_id = $("#back_id").val();
            var noback_reason = $("#noback_reason").val();

            //如果能退款，则把不能退款理由清空
            if(back_id == 0){
                $("#noback_reason").html("");
            }

            if(back_id == 1){
                if(noback_reason.length > 20){
                    layer.alert("退款原因超出字数限制！");return false;
                }
            }


            if (cate == 0) {
                layer.alert('请选择商品分类!');return false;
            }
            if (brand == 0) {
                layer.alert('请选择商品品牌!');return false;
            }
            if (g_name == '') {
                layer.alert('请填写商品名称!'); return false;
            }
            if (brief == '') {
                layer.alert('请填写商品简介!');return false;
            }
            if (img == '') {
                layer.alert('请上传商品图片!', {icon: 8,time:1000});return false;
            }
            if (price == '') {
                layer.alert('请正确填写商品价格!');return false;
            }
            if (weight == '') {
                layer.alert('请正确填写商品重量!');return false;
            }
            if (stock == '') {
                layer.alert('请正确填写商品库存!');return false;
            }
            if (per_num == '') {
                layer.alert('请正确填写装箱件数!');return false;
            }
            if(!goods_imgs) {
                layer.alert('请上传商品图册!');return false;
            }
            $("#editGoodsForm").submit();
        });

        //进来判断一次
        if( $("#back_id").val() == 1){
            $(".back_explain").show();
        }else{
            $(".back_explain").hide();
        }

        //退款理由
        $("#back_id").change(function () {
            if($(this).val() == 1){
                $(".back_explain").show();
            }else{
                $(".back_explain").hide();
            }
        });

        $('#cat_id').change(function(){
            admin_category($(this).val(),'cat_id_2');
            $('#cat_id_3').empty().html("<option value='0'>选择分类</option>");
        })

        $('#cat_id_2').change(function(){
            $id = $(this).val();
            if($id == 0){
                $('#cat_id_3').empty().html("<option value='0'>选择分类</option>");
                return false;
            }
            admin_category($id,'cat_id_3');
        })

        // 插件切换列表
        $('.tab-base').find('.tab').click(function(){
            $('.tab-base').find('.tab').each(function(){
                $(this).removeClass('current');
            });
            $(this).addClass('current');
            var tab_index = $(this).data('index');
            $(".tab_div_1, .tab_div_2, .tab_div_3, .tab_div_4,.tab_div_5").hide();
            $(".tab_div_"+tab_index).show();
        });
    })

    /**
     * 获取多级联动的商品分类
     */
    function admin_category(id,next)
    {
        $.ajax({
            type : "GET",
            url  : "{:U('Category/get_category')}",
            data : {pid :id},
            success: function(v) {
                v = "<option value='0'>选择分类</option>" + v;
                $('#'+next).empty().html(v);
            }
        });
    }

    var ue = UE.getEditor('goods_content',{
        initialFrameWidth: "100%", //初化宽度
        initialFrameHeight:300,
        autoHeightEnabled: true
    });

    /*
     * 以下是图片上传方法
     */
    // 上传商品图片成功回调函数
    function call_back(fileurl_tmp){
        $("#original_img").val(fileurl_tmp);
        $("#original_img2").attr('href', fileurl_tmp);
    }

    // 上传商品相册回调函数
    function call_back2(paths){

        var last_div = $(".goods_xc:last").prop("outerHTML");
        for (var i = 0;i<paths.length ;i++ )
        {
            $(".goods_xc:eq(0)").before(last_div);	// 插入一个 新图片
            $(".goods_xc:eq(0)").find('a:eq(0)').attr('href',paths[i]).attr('onclick','').attr('target', "_blank");// 修改他的链接地址
            $(".goods_xc:eq(0)").find('img').attr('src',paths[i]);// 修改他的图片路径
            $(".goods_xc:eq(0)").find('a:eq(1)').attr('onclick',"ClearPicArr2(this,'"+paths[i]+"')").text('删除');
            $(".goods_xc:eq(0)").find('input').val(paths[i]); // 设置隐藏域 要提交的值
        }
    }

    /*
     * 上传之后删除组图input
     * @access   public
     * @val      string  删除的图片input
     */
    function ClearPicArr2(obj,path)
    {
        $.ajax({
            type:'GET',
            url:"{:U('Uploadify/delupload')}",
            data:{action:"del", filename:path},
            success:function(){
                $(obj).parent().remove(); // 删除完服务器的, 再删除 html上的图片
            }
        });
    }

    function edit_clear_pic(obj,path,id)
    {
        layer.confirm('您确认删除此图片吗？', {
            btn: ['确认','取消']
        }, function(){
            $.ajax({
                url: "{:U('Good/del_pic')}",
                type: "post",
                data: {pic_id: id},
                beforeSend: function () {
                    layer.msg('正在删除...', {icon: 16,time:500});
                },
                success: function (msg) {
                    ClearPicArr2(obj,path);
                }
            });
        });
    }

    function img_call_back(fileurl_tmp)
    {
        //拼接商品图片缩略图
        var arr = fileurl_tmp.split('/');
        var url = arr[arr.length - 1];
        var dir = arr[arr.length - 2];
        var end = './Uploads/good/'+dir+'/small_'+url;
        $('#thumb').val(end);
        $("#imagetext").val(fileurl_tmp);
        $("#img_a").attr('href', fileurl_tmp);
        $("#img_i").attr('onmouseover', "layer.tips('<img src="+fileurl_tmp+">',this,{tips: [1, '#fff']});");
    }
</script>